/*global $, document, Chart, LINECHART, data, options, window*/
$(document).ready(function () {

    //获取当前年份
    var date = new Date();
    var now_year = date.getFullYear();
    var now_month = date.getMonth()+1;
    var now_day = date.getDate();

    $(".today_time").html(now_year + "年" + now_month + "月" + now_day+"日");
    $("#bar_year").html(now_year + "年");
    $("#pie_month").html(now_year + "年" + now_month + "月");
    //page.1
    todaySummary(now_year+"-"+now_month+"-"+now_day);

    //page.2
    $("#totalChartBtn").click(function () {
        // ------------------------------------------------------- //
        // 哪年每月营业额条形表
        // ------------------------------------------------------ //
        moneyChartOfYear(now_year);

        $("#bar_year").html(now_year);
    });

    //page.3
    $("#roomChartBtn").click(function () {
        roomChartOfYear(now_year);
        $("#pie_month").html(now_year + "年" + now_month + "月");
    });
    
    //page.4
    $("#peopleChartBtn").click(function () {
        userChart();
    });
    
    //page.5
    $("#idCardKeyWord").click(function () {
        $("#searchKeyWord").html($("#idCardKeyWord").html());
    });
    $("#roomNumKeyWord").click(function () {
        $("#searchKeyWord").html($("#roomNumKeyWord").html());
    });
    $("#nameKeyWord").click(function () {
        $("#searchKeyWord").html($("#nameKeyWord").html());
    });
    $("#searchBtn").click(function () {
        var context = $("#searchContext").val();
        var searchType = $("#searchKeyWord").html();
        if (searchType == "身份证号") {
            if (context.length != 18) {
                alert("身份证不等于18位，请重新输入！");
            } else {
                searchInfo(searchType, context);
            }
        } else if(searchType == "姓名" || searchType == "房间号"){
            searchInfo(searchType, context);
        }
    });


    //page.1
    //哪年每月营业额条形表弹出时间选择表 改变时间
    $('#changeDayBtn').click(function(){ //假设 test1 是一个按钮
        laydate.render({
            elem: '#today',
            value: new Date(),
            showBottom: false,
            theme: 'default',
            min: '2000-1-1',
            max: '2099-12-31',
            show: true,
            //这里代表的意思是：点击 test1 所在元素阻止关闭事件冒泡。如果不设定，则无法弹出控件
            closeStop: '#changeDayBtn',
            done: function(value){
                var list = value.split("-");
                var time = list[0] + "年" + list[1] + "月" + list[2] + "日";
                $(".today_time").html(time);
                todaySummary(value);
            }
        });
    });

    //page.2
    //哪年每月营业额条形表弹出时间选择表 改变时间
    $('#changeMoneyYearBtn').click(function(){ //假设 test1 是一个按钮
        laydate.render({
            elem: '#moneyYear',
            type: 'year',
            value: new Date(),
            btns:['confirm'],
            theme: 'default',
            min: '2000-1-1',
            max: '2099-12-31',
            show: true,
            //这里代表的意思是：点击 test1 所在元素阻止关闭事件冒泡。如果不设定，则无法弹出控件
            closeStop: '#changeMoneyYearBtn',
            done: function(value){
                var time = value + "年";
                $("#bar_year").html(time);
                moneyChartOfYear(value);
            }
        });
    });

    //page.3
    //哪年每月营业额条形表弹出时间选择表 改变时间
    $('#changeRoomTimeBtn').click(function(){ //假设 test1 是一个按钮
        laydate.render({
            elem: '#roomTimeBtn',
            type: 'month',
            value: new Date(),
            btns:['confirm'],
            theme: 'default',
            min: '2000-1-1',
            max: '2099-12-31',
            show: true,
            //这里代表的意思是：点击 test1 所在元素阻止关闭事件冒泡。如果不设定，则无法弹出控件
            closeStop: '#changeRoomTimeBtn',
            done: function(value){
                var list = value.split("-");
                var time = list[0] + "年" + list[1] + "月";
                $("#pie_month").html(time);
                roomChartOfYear(value);
            }
        });
    });

    //page.5


});

//page.1
function todaySummary(today) {
    $.ajax({
        url: "business/today_summary",
        type: "POST",
        dataType: "json",
        data:{
            today: today
        },
        success : function (data) {
            var todayWorker = data.todayWorker;
            if (data.todayRoomsData != null) {
                getTodaySummary(data.todayRoomsData);
            }
            if (todayWorker != null) {
                $("#connect_phone").prop("href", "tel:"+todayWorker.user_phone);
                $("#worker_name").html(todayWorker.user_account);
                $("#start_work").html(todayWorker.work_time);
                $("#out_work").html(todayWorker.off_work_time);
                if (todayWorker.user_image == null) {
                    $("#head_image").prop("src", "images/user_images/default.png");
                }else {
                    $("#head_image").prop("src", "images/user_images/"+todayWorker.user_image);
                }
            }else {
                $("#connect_phone").prop("href", "tel:");
                $("#worker_name").html("");
                $("#start_work").html("");
                $("#out_work").html("");
                $("#head_image").prop("src", "images/user_images/default.png");
            }
            if (data.todayIncome != null) {
                $("#today_money").html(data.todayIncome);
            }
            var pos = data.checkInfos;
            if (pos != null) {
                var context = '';
                for (var i = 0; i < pos.length; i++) {
                    var po = pos[i];
                    context += '<tr>\n' +
                        '<td>'+po.room_num+'</td>\n' +
                        '<td>'+po.actual_name+'</td>\n' +
                        '<td>'+po.id_card+'</td>\n' +
                        '<td>'+po.check_in_time+'</td>\n' +
                        '<td>'+po.check_out_time+'</td>\n' +
                        '<td>'+po.room_type_name+'</td>\n' +
                        '</tr>';
                }
                $("#todayCheckInInfoContext").html(context);
            }
        }
    });
}

function getTodaySummary(data) {
    var LINECHART1 = $('#todayBarChart');
    var todayBarChart = new Chart(LINECHART1, {
        type: 'bar',
        options: {
            scales: {
                xAxes: [{
                    display: true,
                    gridLines: {
                        display: false
                    }
                }],
                yAxes: [{
                    display: true,
                    gridLines: {
                        display: false
                    },
                    ticks: {
                        beginAtZero:true,
                        stepSize: 1
                    }
                }]
            },
            legend: {
                display: false
            }
        },
        data: {
            labels: ["高级双床房", "豪华双床房", "高级大床房", "豪华大床房", "豪华休闲套房", "豪华海景套房", "超级海景房", "至尊海景套房"],
            datasets: [
                {
                    label: "售出",
                    fill: true,
                    lineTension: 0,
                    backgroundColor: [
                        'rgb(79,166,238)',
                        'rgb(79,166,238)',
                        'rgb(79,166,238)',
                        'rgb(79,166,238)',
                        'rgb(79,166,238)',
                        'rgb(79,166,238)',
                        'rgb(79,166,238)',
                        'rgb(79,166,238)'
                    ],
                    borderColor: [
                        'rgb(236,238,236)',
                        'rgb(236,238,236)',
                        'rgb(236,238,236)',
                        'rgb(236,238,236)',
                        'rgb(236,238,236)',
                        'rgb(236,238,236)',
                        'rgb(236,238,236)',
                        'rgb(236,238,236)'
                    ],
                    borderWidth: 1,
                    pointBorderColor: '#59c2e6',
                    pointHoverBackgroundColor: '#59c2e6',
                    borderCapStyle: 'butt',
                    borderDash: [],
                    borderDashOffset: 0.0,
                    borderJoinStyle: 'miter',
                    pointBackgroundColor: "#59c2e6",
                    pointBorderWidth: 0,
                    pointHoverRadius: 4,
                    pointHoverBorderColor: "#fff",
                    pointHoverBorderWidth: 0,
                    pointRadius: 4,
                    pointHitRadius: 0,
                    data: data,
                    spanGaps: false
                }
            ]
        }
    });
}

//page.2
// function fiveYearMoneyChart(year) {
//     var five_year = [(year - 4)+'', (year - 3)+'', (year - 2)+'', (year - 1)+'', year+''];
//     var data = [2, 2.8, 3, 2.2,3.4];
//     getFiveYearMoneyChart(data,five_year);
// }
//
// function getFiveYearMoneyChart(data,five_year) {
//
//     var LINECHART = $('#allYearMoneyChart');
//     var allYearMoneyChart = new Chart(LINECHART, {
//         type: 'line',
//         options: {
//             scales: {
//                 xAxes: [{
//                     display: true,
//                     gridLines: {
//                         display: false
//                     }
//                 }],
//                 yAxes: [{
//                     display: true,
//                     gridLines: {
//                         display: false
//                     }
//                 }]
//             },
//             legend: {
//                 display: false
//             }
//         },
//         data: {
//             labels: five_year,
//             datasets: [
//                 {
//                     label: "营业额为",
//                     fill: true,
//                     lineTension: 0,
//                     backgroundColor: "transparent",
//                     borderColor: '#6ccef0',
//                     pointBorderColor: '#59c2e6',
//                     pointHoverBackgroundColor: '#59c2e6',
//                     borderCapStyle: 'butt',
//                     borderDash: [],
//                     borderDashOffset: 0.0,
//                     borderJoinStyle: 'miter',
//                     borderWidth: 3,
//                     pointBackgroundColor: "#59c2e6",
//                     pointBorderWidth: 0,
//                     pointHoverRadius: 4,
//                     pointHoverBorderColor: "#fff",
//                     pointHoverBorderWidth: 0,
//                     pointRadius: 4,
//                     pointHitRadius: 0,
//                     data: data,
//                     spanGaps: false
//                 }
//             ]
//         }
//     });
// }

function moneyChartOfYear(year) {
    $.ajax({
        url: "business/year_summary",
        type: "POST",
        dataType: "json",
        data:{
            year: year
        },
        success : function (data) {
            var money = [];
            var month = [];
            var total_price = 0;
            for (var i = 0; i < data.length; i++) {
                var po = data[i];
                total_price += po.month_money;
                money.push(po.month_money);
                month.push(po.month);
            }
            getMoneyChart(month,money);
            $("#total_price").html(total_price);
        }
    });

}

function getMoneyChart(month,money) {
    var LINECHART1 = $('#moneyLineChart');
    var moneyLineChart = new Chart(LINECHART1, {
        type: 'bar',
        options: {
            scales: {
                xAxes: [{
                    display: true,
                    gridLines: {
                        display: false
                    }
                }],
                yAxes: [{
                    display: true,
                    gridLines: {
                        display: false
                    }
                }]
            },
            legend: {
                display: false
            }
        },
        data: {
            labels: month,
            datasets: [
                {
                    label: "营业额",
                    fill: true,
                    lineTension: 0,
                    backgroundColor: [
                        'rgb(121, 106, 238)',
                        'rgb(121, 106, 238)',
                        'rgb(121, 106, 238)',
                        'rgb(121, 106, 238)',
                        'rgb(121, 106, 238)',
                        'rgb(121, 106, 238)',
                        'rgb(121, 106, 238)',
                        'rgb(121, 106, 238)',
                        'rgb(121, 106, 238)',
                        'rgb(121, 106, 238)',
                        'rgb(121, 106, 238)',
                        'rgb(121, 106, 238)'
                    ],
                    borderColor: [
                        'rgb(121, 106, 238)',
                        'rgb(121, 106, 238)',
                        'rgb(121, 106, 238)',
                        'rgb(121, 106, 238)',
                        'rgb(121, 106, 238)',
                        'rgb(121, 106, 238)',
                        'rgb(121, 106, 238)',
                        'rgb(121, 106, 238)',
                        'rgb(121, 106, 238)',
                        'rgb(121, 106, 238)',
                        'rgb(121, 106, 238)',
                        'rgb(121, 106, 238)'
                    ],
                    borderWidth: 1,
                    pointBorderColor: '#59c2e6',
                    pointHoverBackgroundColor: '#59c2e6',
                    borderCapStyle: 'butt',
                    borderDash: [],
                    borderDashOffset: 0.0,
                    borderJoinStyle: 'miter',
                    pointBackgroundColor: "#59c2e6",
                    pointBorderWidth: 0,
                    pointHoverRadius: 4,
                    pointHoverBorderColor: "#fff",
                    pointHoverBorderWidth: 0,
                    pointRadius: 4,
                    pointHitRadius: 0,
                    data: money,
                    spanGaps: false
                }
            ]
        }
    });
}

//page.3
function roomChartOfYear(month) {
    $.ajax({
        url: "business/rooms_data",
        type: "POST",
        dataType: "json",
        data:{
            month: month
        },
        success : function (data) {
            getRoomChart(data.monthRoomsData);
            $("#total_rooms").html(data.totalRooms);
        }
    });
}

function getRoomChart(data) {
    // ------------------------------------------------------- //
    // Pie Chart
    // ------------------------------------------------------ //
    var PIECHARTEXMPLE    = $('#roomPieChart');
    var roomPieChart = new Chart(PIECHARTEXMPLE, {
        type: 'pie',
        options: {
            legend: {
                orient:'vertical'
            }
        },
        data: {
            labels: [
                "高级双床房",
                "豪华双床房",
                "高级大床房",
                "豪华大床房",
                "豪华休闲套房",
                "豪华海景套房",
                "超级海景房",
                "至尊海景套房"
            ],
            datasets: [
                {
                    data: data,
                    borderWidth: [0, 0, 0, 0,0, 0, 0, 0],
                    backgroundColor: [
                        "#0098ff",
                        "#00b7ff",
                        "#00eeff",
                        "#6cffa5",
                        "#51ff00",
                        "#adff00",
                        "#f0ff00",
                        "#ffd600"
                    ],
                    hoverBackgroundColor: [
                        "#0098ff",
                        "#00b7ff",
                        "#00eeff",
                        "#6cffa5",
                        "#51ff00",
                        "#adff00",
                        "#f0ff00",
                        "#ffd600"
                    ]
                }]
        }
    });

    var roomPieChart = {
        responsive: true
    };
}

//page.4
function userChart() {
    $.ajax({
        url: "business/user_data",
        type: "GET",
        dataType: "json",
        success : function (data) {
            var count = 0;
            var level_type = [];
            var level_rate = [];
            for (var i = 0; i < data.length; i++) {
                count += data[i].user_count;
                level_type.push(data[i].level_name);
                level_rate.push(data[i].user_count);
            }
            $("#user_count").html(count);
            getUserChart(level_type,level_rate);
        }
    });
}

function getUserChart(level_type,level_rate) {
    // ------------------------------------------------------- //
    // Pie Chart
    // ------------------------------------------------------ //
    var PIECHARTEXMPLE    = $('#userPieChart');
    var userPieChart = new Chart(PIECHARTEXMPLE, {
        type: 'pie',
        options: {
            legend: {
                orient:'vertical'
            }
        },
        data: {
            labels: level_type,
            datasets: [
                {
                    data: level_rate,
                    borderWidth: 0,
                    backgroundColor: [
                        "#0098ff",
                        "#00b7ff",
                        "#00eeff",
                        "#6cffa5",
                        "#51ff00",
                        "#ffb714",
                        "#f2ff11",
                        "#ff510c",
                        "#f578ff",
                        "#d5b5ff"
                    ],
                    hoverBackgroundColor: [
                        "#0098ff",
                        "#00b7ff",
                        "#00eeff",
                        "#6cffa5",
                        "#51ff00",
                        "#ffb714",
                        "#f2ff11",
                        "#ff510c",
                        "#f578ff",
                        "#d5b5ff"
                    ]
                }]
        }
    });

    var userPieChart = {
        responsive: true
    };
}

//page.5
function searchInfo(searchType,context) {
    $.ajax({
        url: "business/check_info",
        type: "POST",
        dataType: "json",
        data:{
            searchType: searchType,
            context: context
        },
        success : function (data) {
            var context = '';
            for (var i = 0; i < data.length; i++) {
                var po = data[i];
                context += '<tr>\n' +
                    '<td>'+po.room_num+'</td>\n' +
                    '<td>'+po.actual_name+'</td>\n' +
                    '<td>'+po.id_card+'</td>\n' +
                    '<td>'+po.check_in_time+'</td>\n' +
                    '<td>'+po.check_out_time+'</td>\n' +
                    '<td>'+po.room_type_name+'</td>\n' +
                    '</tr>';
            }
            $("#checkInInfoContext").html(context);
        }
    });
}

